// pages/usercenter/index.js
import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import Toast from 'tdesign-miniprogram/toast/index';

const menuData = [
	[{
			title: '帮助中心',
			tit: '',
			url: '',
			type: 'help-center',
		},
		{
			title: '客服热线',
			tit: '',
			url: '',
			type: 'service',
			icon: 'service',
		},
	],
];

const tagInfos = [
	{
		title: '我的评价',
		iconName: 'comment',
		orderNum: 0,
		tabType: 60,
		status: 1,
	},
	{
		title: '点赞',
		iconName: 'exchang',
		orderNum: 0,
		tabType: 0,
		status: 1,
	},
];

const getDefaultData = () => ({
	showMakePhone: false,
	userInfo: {
		avatarUrl: '',
		nickName: '正在登录...',
		phoneNumber: '',
	},
	menuData,
	tagInfos,
	customerServiceInfo: {},
	currAuthStep: 1,
	showKefu: true,
	versionNo: '',
});

Page({

	/**
	 * 页面的初始数据
	 */
	data: getDefaultData(),

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getVersionInfo();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.init();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		this.init();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	},

	init() {
		this.fetUseriInfoHandle();
	},

	fetUseriInfoHandle() {
		fetchUserCenter().then(({
			userInfo,
			countsData,
			tagInfos: orderInfo,
			customerServiceInfo
		}) => {
			// eslint-disable-next-line no-unused-expressions
			menuData?.[0].forEach((v) => {
				countsData.forEach((counts) => {
					if (counts.type === v.type) {
						// eslint-disable-next-line no-param-reassign
						v.tit = counts.num;
					}
				});
			});
			const info = tagInfos.map((v, index) => ({
				...v,
				// ...orderInfo[index],
			}));
			this.setData({
				userInfo,
				menuData,
				tagInfos: info,
				customerServiceInfo,
				currAuthStep: 2,
			});
			wx.stopPullDownRefresh();
		});
	},

	onClickCell({
		currentTarget
	}) {
		const {
			type
		} = currentTarget.dataset;

		switch (type) {
			case 'address': {
				wx.navigateTo({
					url: '/pages/user/address/list/index'
				});
				break;
			}
			case 'service': {
				this.openMakePhone();
				break;
			}
			case 'help-center': {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '你点击了帮助中心',
					icon: '',
					duration: 1000,
				});
				break;
			}
			case 'point': {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '你点击了积分菜单',
					icon: '',
					duration: 1000,
				});
				break;
			}
			case 'coupon': {
				wx.navigateTo({
					url: '/pages/coupon/coupon-list/index'
				});
				break;
			}
			default: {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '未知跳转',
					icon: '',
					duration: 1000,
				});
				break;
			}
		}
	},

	jumpNav(e) {
		const status = e.detail.tabType;

		if (status === 0) {
			wx.navigateTo({
				url: '/pages/order/after-service-list/index'
			});
		} else {
			wx.navigateTo({
				url: `/pages/order/order-list/index?status=${status}`
			});
		}
	},

	openMakePhone() {
		this.setData({
			showMakePhone: true
		});
	},

	closeMakePhone() {
		this.setData({
			showMakePhone: false
		});
	},

	call() {
		wx.makePhoneCall({
			phoneNumber: this.data.customerServiceInfo.servicePhone,
		});
	},

	gotoUserEditPage() {
		
		const {
			currAuthStep
		} = this.data;
		if (currAuthStep === 2) {
			console.log(currAuthStep)
			wx.navigateTo({
				url: '/pages/user/person-info/index'
			});
		} else {
			this.fetUseriInfoHandle();
		}
	},

	getVersionInfo() {
		const versionInfo = wx.getAccountInfoSync();
		const {
			version,
			envVersion = __wxConfig
		} = versionInfo.miniProgram;
		this.setData({
			versionNo: envVersion === 'release' ? version : envVersion,
		});
	},
})